//
//  MessageDetailViewController.m
//  Youhuiju
//
//  Created by 余尚飞 on 16/8/30.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "MessageDetailViewController.h"
//跳转界面

//自定义视图
#import "MessageTopView.h"
#import "MessDetailCollectionReusableView.h"

//自定义cell
#import "MessDetailCollectionViewCell.h"
//

@interface MessageDetailViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)MessageTopView *topView;//顶部视图
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;




@end



@implementation MessageDetailViewController

static NSString *MessDetailCollectionViewCellIden = @"MessDetailCollectionViewCellIden";
static NSString *MessDetailCollectionReusableViewIden = @"MessDetailCollectionReusableViewIden";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self load_UI];
    
    // Do any additional setup after loading the view from its nib.
}
#pragma mark ----- load_UI
-(void)load_UI
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self topView];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"MessDetailCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:MessDetailCollectionViewCellIden];
    [self.collectionView registerNib:[UINib nibWithNibName:@"MessDetailCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:MessDetailCollectionReusableViewIden];
}

#pragma mark ----- collectionView delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(345 * FitW, 118 * FitH);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    
    return CGSizeMake(self.collectionView.frame.size.width, 46 * FitH);
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//    UICollectionReusableView *reusableView = nil;
    UICollectionReusableView *reusableView = [[UICollectionReusableView alloc]init];
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        MessDetailCollectionReusableView * header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:MessDetailCollectionReusableViewIden forIndexPath:indexPath];
//        header.backgroundColor = [UIColor colorWithHexString:@"F5F5F5"];
        //有缓存了
        //        [header.imgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kDataServer2,_topic_img_url]] placeholderImage:[UIImage imageNamed:@"AppIcon60x60@3x"]];
        
//        header.sectionLabel.text = self.dataSourceArray[indexPath.section];
        
        reusableView = header;
    }
    
    
    return reusableView;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 10 * FitH, 0);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MessDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MessDetailCollectionViewCellIden forIndexPath:indexPath];
    cell.layer.cornerRadius = 5 * FitH;
    cell.layer.borderWidth = 1.0f * FitH;
    cell.layer.borderColor = [UIColor colorWithHexString:@"DFDFDF"].CGColor;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DLog(@"点击进入到相应的消息内容");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ----- lazy loading
-(MessageTopView *)topView
{
    WeakObj(self);
    if (!_topView)
    {
        _topView = [[MessageTopView alloc]init];
        [self.view addSubview:_topView];
        [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            make.top.equalTo(self.view.mas_top).offset(20);
            make.height.mas_equalTo(@44);
        }];
        
        _topView.backButtonClickedHandler = ^{
            [weakself.navigationController popViewControllerAnimated:YES];
        };
    }
    return _topView;
}


@end
